package com.xiaoluo.lottery.common.weixin.base;

import com.xiaoluo.lottery.common.weixin.utils.Configure;
import com.xiaoluo.lottery.common.weixin.utils.RandomStringGenerator;
import com.xiaoluo.lottery.common.weixin.utils.Signature;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 * Project : lottery
 * Package : com.xiaoluo.lottery.common.weixin.pay.beans
 * Created by Willian.
 * Date  : 2016/4/6 20:10.
 */
@Root(name = "xml")
public class BaseReq {

    @Element(name = "appid")        private String appid;       //     公众账号ID	appid	是	String(32)	wxd678efh567hg6787	微信分配的公众账号ID（企业号corpid即为此appId）
    @Element(name = "mch_id")       private String mch_id;       //     商户号	    mch_id	是	String(32)	1230000109	微信支付分配的商户号
    @Element(name = "nonce_str")    private String nonce_str;    //     随机字符串	   nonce_str	是	String(32)	5K8264ILTKCH16CQ2502SI8ZNMTM67VS	随机字符串，不长于32位。推荐随机数生成算法
    @Element(name = "sign")         private String sign;        //     签名	       sign	是	String(32)	C380BEC2BFD727A4B6845133519F3AD6	签名，详见签名生成算法


    public BaseReq(){
        super();
        init();
    }
    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {

        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    /**
     * 初始化系统配置
     */
    public void init(){
        setAppid(Configure.getAppid());
        setMch_id(Configure.getMchid());
    }

    /**
     * 生成签名信息
     */
    public <T> T builder(T obj){
        //生成随机数
        setNonce_str(RandomStringGenerator.getRandomStringByLength(32));

        try {
            //生成签名
            setSign(Signature.getSign(obj));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }
}
